home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSCIE.C < prev    next >
C/C++ Source or Header  |  1993-05-18  |  15KB  |  432 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gscie.c */
  20. /* CIE color rendering for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxrefct.h"            /* early for gscie.h */
  24. #include "gxcolor.h"            /* early for gscolor2.h */
  25. #include "gscspace.h"
  26. #include "gscolor2.h"            /* for gs_set/currentcolorrendering */
  27. #include "gscie.h"
  28. #include "gxarith.h"
  29. #include "gxdevice.h"            /* for gs_color_index */
  30. #include "gzcolor.h"
  31. #include "gzstate.h"
  32.  
  33. /* Forward references */
  34. private void near cie_mult3(P3(const gs_vector3 *, const gs_matrix3 *, gs_vector3 *));
  35. private void near cie_matrix_mult3(P3(const gs_matrix3 *, const gs_matrix3 *, gs_matrix3 *));
  36. private void near cie_invert3(P2(const gs_matrix3 *, gs_matrix3 *));
  37. private void near cie_restrict3(P3(const gs_vector3 *, const gs_range3 *, gs_vector3 *));
  38. private void near cie_lookup3(P3(const gs_vector3 *, const gx_cie_cache *, gs_vector3 *));
  39. private void near cie_matrix_init(P1(gs_matrix3 *));
  40.  
  41. #define lookup(vin, pcache, vout)\
  42.   if ( (pcache)->is_identity ) vout = (vin);\
  43.   else vout = (pcache)->values[(int)(((vin) - (pcache)->base) * (pcache)->factor)]
  44.  
  45. #define restrict(vin, range, vout)\
  46.   if ( (vin) < (range).rmin ) vout = (range).rmin;\
  47.   else if ( (vin) > (range).rmax ) vout = (range).rmax;\
  48.   else vout = (vin)
  49.  
  50. /* ------ Default values for CIE dictionary elements ------ */
  51.  
  52. /* Default transformation procedures. */
  53.  
  54. private int
  55. a_identity(const float *in, const gs_cie_a *pcie, float *out)
  56. {    *out = *in;
  57.     return 0;
  58. }
  59. private int
  60. abc_identity(const gs_vector3 *in, const gs_cie_abc *pcie, gs_vector3 *out)
  61. {    *out = *in;
  62.     return 0;
  63. }
  64. private int
  65. common_identity(const gs_vector3 *in, const gs_cie_common *pcie, gs_vector3 *out)
  66. {    *out = *in;
  67.     return 0;
  68. }
  69. private int
  70. render_identity(const gs_vector3 *in, const gs_cie_render *pcie, gs_vector3 *out)
  71. {    *out = *in;
  72.     return 0;
  73. }
  74. private int
  75. tpqr_identity(const gs_vector3 *in, const gs_cie_wbsd *pwbsd, const gs_cie_render *pcie, gs_vector3 *out)
  76. {    *out = *in;
  77.     return 0;
  78. }
  79. private int
  80. render_table_identity(const byte *in, int m, const gs_cie_render *pcie, float *out)
  81. {    int j;
  82.     for ( j = 0; j < m; j++ ) out[j] = in[j] / 255.0;
  83.     return 0;
  84. }
  85.  
  86. /* Default vectors and matrices. */
  87.  
  88. const gs_range3 Range3_default = { {0,1}, {0,1}, {0,1} };
  89. const gs_cie_abc_proc3 DecodeABC_default = abc_identity;
  90. const gs_cie_common_proc3 DecodeLMN_default = common_identity;
  91. const gs_matrix3 Matrix3_default = { {1,0,0}, {0,1,0}, {0,0,1}, 1 };
  92. const gs_range RangeA_default = {0,1};
  93. const gs_cie_a_proc DecodeA_default = a_identity;
  94. const gs_vector3 MatrixA_default = { 1, 1, 1 };
  95. const gs_vector3 BlackPoint_default = { 0, 0, 0 };
  96. const gs_cie_render_proc3 Encode_default = render_identity;
  97. const gs_cie_transform_proc3 TransformPQR_default = tpqr_identity;
  98. const gs_cie_render_table_proc RenderTableT_default = render_table_identity;
  99.  
  100. /* setcolorrendering */
  101. int
  102. gs_setcolorrendering(gs_state *pgs, gs_cie_render *pcie)
  103. {    int code = gs_cie_render_init(pcie);
  104.     if ( code < 0 ) return code;
  105.     rc_assign(pgs->cie_render, pcie, pgs->memory_procs,
  106.           "gs_setcolorrendering");
  107.     /* Initialize the joint caches, if needed, */
  108.     /* by re-installing the color space. */
  109.     (*pgs->color_space->type->install_cspace)(pgs->color_space, pgs);
  110.     return gx_remap_color(pgs);
  111. }
  112.  
  113. /* currentcolorrendering */
  114. const gs_cie_render *
  115. gs_currentcolorrendering(const gs_state *pgs)
  116. {    return pgs->cie_render;
  117. }
  118.  
  119. /* Get the joint caches, to avoid having to import gzstate.h */
  120. gx_cie_joint_caches *
  121. gx_currentciecaches(gs_state *pgs)
  122. {    return pgs->cie_joint_caches;
  123. }
  124.  
  125. /* ------ Complete a rendering structure ------ */
  126.  
  127. int
  128. gs_cie_render_init(gs_cie_render *pcie)
  129. {    gs_matrix3 PQR_inverse;
  130.     cie_matrix_init(&pcie->MatrixLMN);
  131.     cie_matrix_init(&pcie->MatrixABC);
  132.     cie_matrix_init(&pcie->MatrixPQR);
  133.     cie_invert3(&pcie->MatrixPQR, &PQR_inverse);
  134.     cie_matrix_mult3(&PQR_inverse, &pcie->MatrixLMN, &pcie->MatrixPQR_inverse_LMN);
  135.     cie_mult3(&pcie->points.WhitePoint, &pcie->MatrixPQR, &pcie->wdpqr);
  136.     cie_mult3(&pcie->points.BlackPoint, &pcie->MatrixPQR, &pcie->bdpqr);
  137.     /****** FINISH ******/
  138.     return 0;
  139. }
  140.  
  141. /* ------ Fill in the joint cache ------ */
  142.  
  143. int
  144. gx_cie_joint_caches_init(gx_cie_joint_caches *pjc,
  145.   const gs_cie_common *pcie, const gs_cie_render *pcier)
  146. {    pjc->points_sd.ws.xyz = pcie->points.WhitePoint;
  147.     cie_mult3(&pjc->points_sd.ws.xyz, &pcier->MatrixPQR, &pjc->points_sd.ws.pqr);
  148.     pjc->points_sd.bs.xyz = pcie->points.BlackPoint;
  149.     cie_mult3(&pjc->points_sd.bs.xyz, &pcier->MatrixPQR, &pjc->points_sd.bs.pqr);
  150.     pjc->points_sd.wd.xyz = pcier->points.WhitePoint;
  151.     pjc->points_sd.wd.pqr = pcier->wdpqr;
  152.     pjc->points_sd.bd.xyz = pcier->points.BlackPoint;
  153.     pjc->points_sd.bd.pqr = pcier->bdpqr;
  154.     /****** FINISH ******/
  155.     return 0;
  156. }
  157.  
  158. /* ------ Remap (render) a CIE color (using the caches). ------ */
  159.  
  160. private int near cie_remap_finish(P4(const gs_vector3 *,
  161.   const gs_cie_common *, gx_device_color *, gs_state *));
  162.  
  163. /* Render a CIEBasedABC color. */
  164. int
  165. gx_remap_CIEBasedABC(const gs_client_color *pc, const gs_color_space *pcs,
  166.   gx_device_color *pdc, gs_state *pgs)
  167. {    const gs_cie_abc *pcie = pcs->params.abc;
  168.     gs_vector3 abc, lmn;
  169.     cie_restrict3((const gs_vector3 *)&pc->paint.values[0], &pcie->RangeABC, &abc);
  170.         /* (*pcie->DecodeABC)(&abc, pcie, &abc); */
  171.     cie_lookup3(&abc, &pcie->caches.DecodeABC[0], &abc);
  172.     cie_mult3(&abc, &pcie->MatrixABC, &lmn);
  173.     return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
  174. }
  175.  
  176. /* Render a CIEBasedA color. */
  177. int
  178. gx_remap_CIEBasedA(const gs_client_color *pc, const gs_color_space *pcs,
  179.   gx_device_color *pdc, gs_state *pgs)
  180. {    const gs_cie_a *pcie = pcs->params.a;
  181.     const gx_cie_cache *pcache = &pcie->caches.DecodeA;
  182.     float a;
  183.     gs_vector3 lmn;
  184.     restrict(pc->paint.values[0], pcie->RangeA, a);
  185.         /* (*pcie->DecodeA)(&a, pcie, &a); */
  186.     lookup(a, pcache, a);
  187.     lmn.u = a * pcie->MatrixA.u;
  188.     lmn.v = a * pcie->MatrixA.v;
  189.     lmn.w = a * pcie->MatrixA.w;
  190.     return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
  191. }
  192.  
  193. /* Common rendering code. */
  194. private int near
  195. cie_remap_finish(const gs_vector3 *plmn, const gs_cie_common *pcommon,
  196.   gx_device_color *pdc, gs_state *pgs)
  197. {    const gs_cie_render *pcie = pgs->cie_render;
  198.     const byte **table;
  199.     gs_vector3 abc, lmn, xyz, pqr;
  200.     gs_client_color cc;
  201.     gs_color_space cs;
  202.  
  203.         /* Finish decoding. */
  204.  
  205.     cie_restrict3(plmn, &pcommon->RangeLMN, &lmn);
  206.         /* (*pcommon->DecodeLMN)(&lmn, pcommon, &lmn); */
  207.     cie_lookup3(&lmn, &pcommon->caches.DecodeLMN[0], &lmn);
  208.     cie_mult3(&lmn, &pcommon->MatrixLMN, &xyz);
  209.  
  210.         /* Render. */
  211.  
  212.     if ( pcie == 0 )        /* default rendering */
  213.     {    abc = xyz;
  214.         table = 0;
  215.     }
  216.     else
  217.     {    const gx_cie_joint_caches *pjc = pgs->cie_joint_caches;
  218.         cie_mult3(&xyz, &pcie->MatrixPQR, &pqr);
  219.         cie_restrict3(&pqr, &pcie->RangePQR, &pqr);
  220.             /* (*pcie->TransformPQR)(&pqr, &pjc->points_sd, pcie, &pqr); */
  221.         cie_lookup3(&pqr, &pjc->TransformPQR[0], &pqr);
  222.         cie_mult3(&pqr, &pcie->MatrixPQR_inverse_LMN, &lmn);
  223.             /* (*pcie->EncodeLMN)(&lmn, pcie, &lmn); */
  224.         cie_lookup3(&lmn, &pcie->caches.EncodeLMN[0], &lmn);
  225.         cie_restrict3(&lmn, &pcie->RangeLMN, &lmn);
  226.         cie_mult3(&lmn, &pcie->MatrixABC, &abc);
  227.             /* (*pcie->EncodeABC)(&abc, pcie, &abc); */
  228.         cie_look